function onoff( elmid ){
	var chk_elm = document.getElementById( elmid+'ONOFF' );
	var onoff_elm = document.getElementById( elmid );
	if( chk_elm.checked ){
		onoff_elm.style.display = 'block';
	}else{
		onoff_elm.style.display = 'none';
	}
}

function addHTML( moto , saki ){
	var motoElm = document.getElementById(moto) ;
	var sakiElm = document.getElementById(saki) ;
	var nodeNum = sakiElm.childNodes.length ;
	var nd = document.createElement('div') ;
	var idName = 'categadd'+nodeNum ;
	nd.setAttribute('id',idName) ;
	sakiElm.appendChild( nd ) ;
	
	var newElm = document.getElementById(idName) ;
	newElm.innerHTML  = motoElm.innerHTML ;
	newElm.innerHTML += "<a href='javascript:void(0)' onclick='delThis(\""+idName+"\");'>DELETE</a>" ;
	//newElm.innerHTML += "<br />" ;
}

function delThis(id){
	var elm = document.getElementById(id) ;
	for(i=0;i<elm.childNodes.length;i++){
		elm.removeChild(elm.childNodes.item(i));
	}
	elm.style.height = '0' ;
}

var menu_open_close_flag = false;
function menu_open_close(cl, mydirname)
{
	if (menu_open_close_flag == false) {
		menu_open_close_flag = true;
		_menu_open_close(cl, mydirname);
	}
}

var menu_max_num = 9;
var menu_num = false;
var plus_flag;
function _menu_open_close(cl, mydirname)
{
	if (cl == true) menu_num = false;
	if (menu_num === false) {
		if (document.getElementById("links_menu1").style.display == 'none') {
			menu_num = 0;
			plus_flag = 1;
		} else {
			menu_num = menu_max_num;
			plus_flag = 0;
		}
		links_setCookie(mydirname +'_sub_menu', plus_flag, 365)
	}
	if (plus_flag == 1) {
		menu_num++;
		if (menu_num == menu_max_num) {
			menu_open_close_flag = false;
			return;
		}
	} else {
		menu_num--;
		if (menu_num == 0) {
			menu_open_close_flag = false;
			return;
		}
	}

	if (document.getElementById("links_menu"+ menu_num)) {
		jQuery("#links_menu"+ menu_num).slideToggle("fast");
		waiting_time = 300;
	} else {
		waiting_time = 0;
	}
	setTimeout("_menu_open_close()", waiting_time);
}

function menu_close(cl)
{
	if (cl == true) menu_num = menu_max_num;
	menu_num--;
	if (document.getElementById("links_menu"+ menu_num)) {
		document.getElementById("links_menu"+ menu_num).style.display = 'none';
	}
	if (menu_num == 0 ) {
		return;
	} else {
		menu_close();
	}
}

function links_setCookie(cname, cvalue, expidate)
{
	var expi = new Date();
	expi.setTime(expi.getTime()+(expidate*24*60*60*1000));
	var setItem = "@" + cname + "=" + escape(cvalue) + ";";
	var Expires = "expires="+expi.toGMTString();
	var Path = "; path=/";
	document.cookie =  setItem + Expires + Path ;
}
function links_getCookie(cname)
{
	myCookie = "@" + cname + "=";
	myValue = null;
	myStr = document.cookie + ";" ;
	myOfst = myStr.indexOf(myCookie);
	if (myOfst != -1){
		myStart = myOfst + myCookie.length;
		myEnd   = myStr.indexOf(";" , myStart);
		myValue = unescape(myStr.substring(myStart,myEnd));
	}
	return myValue;
}

var temp_rating = null;
function linksVote(lid, rating)
{
	if (confirm(sprintf(rating_confirm_msg, {'rating':rating}))) {
		temp_rating = rating;
		for (i=0; i<=10; i++) {
		//	if (i == rating) {
		//		jQuery("#vote_img_"+ lid +"_"+ rating).attr({'src':vote_img_arr[rating].src});
		//	}
			jQuery("#vote_img_"+ lid +"_"+ i).attr({'onMouseOver':''});
			jQuery("#vote_img_"+ lid +"_"+ i).attr({'onMouseOut':''});
			jQuery("#vote_"+ lid +"_"+ i).attr({'onclick':''});
		}
		sendForm('rating', lid)
	} else {
		return false;
	}
}

var vote_img_arr = new Array();
var vote_n_img_arr = new Array();
var save_rating_type;
//var save_mode = new Array();
function openLinksForm(mode, lid, rating_type)
{
	save_rating_type = rating_type;

	/*if (jQuery("#linksForm"+ lid).css("display") == 'block' && save_mode[lid] != mode) {
	//	jQuery("#linksForm"+ lid).slideToggle("fast");
		jQuery("#linksForm"+ lid).css("display", "none");
		renderContent(mode, lid, rating_type);
	} else*/ if (jQuery("#linksForm"+ lid).css("display") == "none") {
		renderContent(mode, lid, rating_type);
	}
	jQuery("#linksForm"+ lid).slideToggle("noraml");
}

function renderContent(mode, lid, rating_type)
{
	if (posted[lid] == undefined) posted[lid] = new Array();
	if (posted[lid][mode] == undefined) {
		var content = '<form>';

		if (mode == 'request') {
			content += '<textarea name="description" id="template_description'+ lid +'" cols="60" rows="5"></textarea><br />';
		}
		if (mode == 'rating' && rating_type == 1) {
			make_vote_img();
			content += rating_vote_msg;
			for (i=0; i<=10; i++) {
				content += '<a href="#vote'+ i +'" id="vote_'+ lid +'_'+ i +'" onClick="linksVote('+ lid +','+ i +');">';
				content += '<img src="'+ vote_n_img_arr[i].src +'" title="'+ i +'" alt="'+ i +'" ';
				content += 'onMouseOver="this.src=\''+ vote_img_arr[i].src +'\'" ';
				content += 'onMouseOut="this.src=\''+ vote_n_img_arr[i].src +'\'" id="vote_img_'+ lid +'_'+ i +'" />';
				content += '</a> '
			}
		} else {
			content += '<input type="button" name="submit" id="template_submit'+ lid +'" value="" ';
			content += 'onClick="sendForm(\''+ mode +'\','+ lid +');">';
		}
		content += '</form>';
		content += '<div id="template_msg'+ lid +'"></div>';
		jQuery("#linksForm"+ lid).html(content);
		if (!(mode == 'rating' && rating_type == 1)) {
			jQuery("#template_submit"+ lid).val(submit_btn[mode+rating_type]);
		}
	} else {
		jQuery("#linksForm"+ lid).html(finished_msg);
	}
	//save_mode[lid] = mode;
}

function make_vote_img()
{
	if (! (vote_img_arr.length==11 && vote_n_img_arr.length==11)) {
		for (i=0; i<=10; i++) {
			vote_img_arr[i] = new Image();
			vote_img_arr[i].src = links_url +'images/rating/vote'+ i +'.png';
			vote_n_img_arr[i] = new Image();
			vote_n_img_arr[i].src = links_url +'images/rating/vote'+ i +'n.png';
		}
	}
}


var posted = new Array();
function sendForm(mode, lid)
{
	if (posted[lid][mode] == true) {
		return false;
	}
	var post_data = {
		'lid'            :lid,
		'XOOPS_G_TICKET' :gticket[mode]
	};

	if (mode == 'request') {
		var description = jQuery("#template_description"+ lid).val();
		if (description == '') return false;
		var add_desc = {'description':description};
		jQuery.extend(post_data, add_desc);
	}

	if (mode == 'rating') {
		if (save_rating_type == 2) {
			temp_rating = 10;
		}
		if (temp_rating === null || temp_rating < 0 || temp_rating > 10) return false;
		var add_rating = {'rating':temp_rating};
		jQuery.extend(post_data, add_rating);
	//	temp_rating = null;
	}

	if (!(mode == 'rating' && save_rating_type == 1)) {
		jQuery("#template_submit"+ lid).attr({'onclick':''});
	}

	if (posted[lid][mode] == undefined) {
		posted[lid][mode] = true;
	}

	jQuery.post(make_send_url(mode), post_data, after_send);
}

function after_send(rtn)
{
	var rtn = eval(rtn);
	var lid = rtn[0].lid;
	var mode = rtn[0].mode;
	jQuery("#template_msg"+ lid).html(rtn[0].msg);
	if (rtn[0].status == 'ERR') {
	//	jQuery("#template_msg"+ lid).css({'color':'red;'})
		jQuery("#template_msg"+ lid).attr({'style':'color:red;'})
	} 
	gticket[mode] = rtn[0].gticket;
	// Rating Num Active
	if (rtn[0].mode == 'rating' && save_rating_type == 1 && temp_rating >= 0 && temp_rating <= 10) {
		jQuery("#vote_img_"+ lid +"_"+ temp_rating).attr({'src':vote_img_arr[temp_rating].src});
		temp_rating = null;
	}
}

function make_send_url(controller)
{
	var url = links_url +'index.php?c='+ controller +'&m=post_j';
	return url;
}

function sprintf( template, obj ){
	var rslt = template;
	for( var Name in obj ){
		value = obj[Name];
		reg = new RegExp("%" + Name + "%", "g");
		rslt = rslt.replace(reg, value);
	}
	return rslt;
}

jQuery.noConflict();